package com.titaniume.java;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Collection;

/**
 *@program: JavaSenior
 *@description:
 *
 * JDK5.0 新增了foreach循环 用于遍历集合、数组
 *
 *@author: renfei.huang
 *@create: 2021-10-11 15:04
 */
public class ForTest {


    @Test
    public void test1(){
        Collection collection = new ArrayList();
        collection.add("aaaa");
        collection.add(123);
        collection.add(456);
        collection.add( new Person("jerry", 20) );
        collection.add(new String("tom") );
        collection.add(true);


        //for(集合中元素类型 局部变量 : 集合对象 )
        for(Object obj : collection){
            System.out.println(obj);
        }


//        collection.forEach(item->
//                System.out.println(item));
    }


    @Test
    public  void test2(){

        int [] arr = new int []{1,3,4,5,6,79};
        //for(数组元素的类型 局部变量: 数组对象)
        for (int i : arr){
            System.out.println(i);
        }
    }

    @Test
    public  void  test3(){

        String [] arr = new String[]{"aa","bb","cc"};


        //方式一：普通for赋值
//        for (int i = 0; i <arr.length ; i++) {
//            arr[i] ="test";
//        }


        //方式二：增强for循环
        for (String s : arr)
        {
            //不会改变
            s ="aaaaa";
        }

        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
    }
}
